{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d61901b5-e20d-47d7-a6f5-6f9fba194d65",
   "metadata": {},
   "source": [
    "This notebook classifies the test set for whether or not documents are related to Trump. These labels are used to produce the numbers in section 3.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b0e568d2-14ae-4838-aa56-fe0ba32c6875",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from transformers import pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bd6cee0-6e70-48fe-8bf0-e1cb15fe497f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('../data/trump_test_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "795eba6b-0c6c-4b94-862f-2f2f9e6e83d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import model for topic classification\n",
    "classifier = pipeline(\"zero-shot-classification\", model='mlburnham/deberta-v3-large-polistance-affect-v1.1', device = 0, batch_size = 32)\n",
    "\n",
    "# entailment template\n",
    "template = 'This text is about {}.'\n",
    "# docs to be classified\n",
    "samples = list(df['text'])\n",
    "# Label the documents\n",
    "res = classifier(samples, ['Trump'], hypothesis_template = template, multi_label = False)\n",
    "# extract entailment probability\n",
    "res = [lab['scores'][0] for lab in res]\n",
    "# Code as 1 if probability >= 0.5\n",
    "res = [1 if lab >= 0.5 else 0 for lab in res]\n",
    "# add topic labels to dataframe\n",
    "df['trump_topic'] = res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e645f655-afae-49ec-9c9d-4026fa1832df",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('../data/trump_test_data.csv', index = False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
